package com.ruoyi.quartz.task;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpClientUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.OaOrg;
import com.ruoyi.system.domain.OaOrgUser;
import com.ruoyi.system.service.IOaOrgService;
import com.ruoyi.system.service.IOaOrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private IOaOrgService oaOrgService;

    @Autowired
    private IOaOrgUserService oaOrgUserService;

    public void ryParams(String params)throws Exception
    {
        String result = HttpUtils.sendGet("https://11.11.155.10/cnodc/exportOrgAndUser","groupId=1694");
        result= result.replaceAll("/n","");
        JSONObject.JSONArray jsonArray = JSON.unmarshal(result,JSONObject.JSONArray.class);
        if (null!= jsonArray && jsonArray.size()>0){
            oaOrgService.truncateTable();
            oaOrgUserService.truncateTable();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject data = (JSONObject) jsonArray.get(i);
                if (data.get("type").equals("group")){//组织机构
                    // OaOrg temp= oaOrgService.selectOaOrgById()
                    OaOrg oaOrg = new OaOrg();
                    oaOrg.setType(data.getStr("type"));
                    oaOrg.setPath(data.getStr("path"));

                    JSONObject info = data.getObj("data");
                    oaOrg.setId(info.getLong("id"));
                    oaOrg.setParentId(info.getLong("parentId"));
                    oaOrg.setCode(info.getStr("code"));
                    oaOrg.setName(info.getStr("name"));
                    if (StringUtils.isNotEmpty(info.getStr("description"))){
                        oaOrg.setDescription(info.getStr("description"));
                    }
                    oaOrg.setWeight(info.getStr("weight"));
                    oaOrg.setRfcode(info.getObj("props").getStr("rfcode"));
                    oaOrg.setTenantId(info.getObj("props").getStr("tenantid"));
                    oaOrgService.insertOaOrg(oaOrg);

                }else{//人员
                    OaOrgUser oaOrgUser = new OaOrgUser();
                    oaOrgUser.setType(data.getStr("type"));
                    oaOrgUser.setPath(data.getStr("path"));

                    JSONObject info = data.getObj("data");
                    oaOrgUser.setId(info.getLong("id"));
                    oaOrgUser.setParentId(info.getLong("parentId"));
                    oaOrgUser.setName(info.getStr("name"));

                    if (StringUtils.isNotEmpty(info.getStr("description"))){
                        oaOrgUser.setDescription(info.getStr("description"));
                    }
                    String email = info.getStr("email");
                    if (StringUtils.isNotEmpty(email)){
                        String[] temps = email.split("@");
                        if (temps.length>0){
                            oaOrgUser.setAccount(temps[0]);
                        }
                    }else{
                        continue;
                    }
                    if (!oaOrgUser.getAccount().equals(info.getStr("account"))){
                        continue;
                    }
                    oaOrgUser.setEmail(email);
                    if (StringUtils.isNotEmpty(info.getStr("phone"))){
                        oaOrgUser.setPhone(info.getStr("phone"));
                    }
                    if (StringUtils.isNotEmpty(info.getStr("fax"))){
                        oaOrgUser.setFax(info.getStr("fax"));
                    }
                    oaOrgUser.setEnabled(String.valueOf(info.getBool("enabled")));
                    oaOrgUser.setWeight(info.getStr("weight"));
                    oaOrgUser.setStatus(info.getStr("status"));


                    JSONObject props= info.getObj("props");
                    oaOrgUser.setMobile(props.getStr("mobile"));
                    oaOrgUser.setLev(props.getStr("lev"));
                    oaOrgUser.setIdcard(props.getStr("idcard"));
                    oaOrgUser.setTenantId(props.getStr("tenantid"));
                    oaOrgUser.setAddress(props.getStr("xjmdd"));
                    oaOrgUser.setInphone(props.getStr("mobile"));
                    oaOrgUser.setOutphone(props.getStr("mobileOut"));
                    oaOrgUser.setUsername(props.getStr("username"));
                    oaOrgUserService.insertOaOrgUser(oaOrgUser);

                }
            }
        }
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void updateOrgAndUser() throws Exception {
        String result = HttpUtils.sendSSLPost("https://11.11.155.10/cnodc/exportOrgAndUser","groupId=1694");
        result= result.replaceAll("/n","");
        JSONObject.JSONArray jsonArray = JSON.unmarshal(result,JSONObject.JSONArray.class);
        if (null!= jsonArray && jsonArray.size()>0){
            oaOrgService.truncateTable();
            oaOrgUserService.truncateTable();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject data = (JSONObject) jsonArray.get(i);
                if (data.get("type").equals("group")){//组织机构
                   // OaOrg temp= oaOrgService.selectOaOrgById()
                    OaOrg oaOrg = new OaOrg();
                    oaOrg.setType(data.getStr("type"));
                    oaOrg.setPath(data.getStr("path"));

                    JSONObject info = data.getObj("data");
                    oaOrg.setId(info.getLong("id"));
                    oaOrg.setParentId(info.getLong("parentId"));
                    oaOrg.setCode(info.getStr("code"));
                    oaOrg.setName(info.getStr("name"));
                    if (StringUtils.isNotEmpty(info.getStr("description"))){
                        oaOrg.setDescription(info.getStr("description"));
                    }
                    oaOrg.setWeight(info.getStr("weight"));
                    oaOrg.setRfcode(info.getObj("props").getStr("rfcode"));
                    oaOrg.setTenantId(info.getObj("props").getStr("tenantid"));
                    oaOrgService.insertOaOrg(oaOrg);

                }else{//人员
                    OaOrgUser oaOrgUser = new OaOrgUser();
                    oaOrgUser.setType(data.getStr("type"));
                    oaOrgUser.setPath(data.getStr("path"));

                    JSONObject info = data.getObj("data");
                    oaOrgUser.setId(info.getLong("id"));
                    oaOrgUser.setParentId(info.getLong("parentId"));
                    oaOrgUser.setName(info.getStr("name"));

                    if (StringUtils.isNotEmpty(info.getStr("description"))){
                        oaOrgUser.setDescription(info.getStr("description"));
                    }
                    String email = info.getStr("email");
                    if (StringUtils.isNotEmpty(email)){
                        String[] temps = email.split("@");
                        if (temps.length>0){
                            oaOrgUser.setAccount(temps[0]);
                        }
                    }else{
                        continue;
                    }
                    if (!oaOrgUser.getAccount().equals(info.getStr("account"))){
                        continue;
                    }
                    oaOrgUser.setEmail(email);
                    if (StringUtils.isNotEmpty(info.getStr("phone"))){
                        oaOrgUser.setPhone(info.getStr("phone"));
                    }
                    if (StringUtils.isNotEmpty(info.getStr("fax"))){
                        oaOrgUser.setFax(info.getStr("fax"));
                    }
                    oaOrgUser.setEnabled(String.valueOf(info.getBool("enabled")));
                    oaOrgUser.setWeight(info.getStr("weight"));
                    oaOrgUser.setStatus(info.getStr("status"));


                    JSONObject props= info.getObj("props");
                    oaOrgUser.setMobile(props.getStr("mobile"));
                    oaOrgUser.setLev(props.getStr("lev"));
                    oaOrgUser.setIdcard(props.getStr("idcard"));
                    oaOrgUser.setTenantId(props.getStr("tenantid"));
                    oaOrgUser.setAddress(props.getStr("xjmdd"));
                    oaOrgUser.setInphone(props.getStr("mobile"));
                    oaOrgUser.setOutphone(props.getStr("mobileOut"));
                    oaOrgUser.setUsername(props.getStr("username"));
                    oaOrgUserService.insertOaOrgUser(oaOrgUser);

                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new RyTask().updateOrgAndUser();
    }
}
